﻿using UnityEngine;
using System.Collections;

public class EnemyFight : Fight {

    public float attackFrequency = 2f;
    float timer;

	// Use this for initialization
	void Start () {
        base.Start();
        FindOpponent("Player");
	}
	
	// Update is called once per frame
	void Update () {

        timer += Time.deltaTime;
        if (!Dead)
        {
            base.Update();
            if (timer > attackFrequency)
            {
                StartAttack();
                timer = 0;
            }
            if (HealthBar.Health < 1)
            {
                Dead = true;
                Camera.main.GetComponent<GUIClass>().EnemiesKilled++;
                Instantiate(self, initialPos, Quaternion.identity);
            }
        }
        else
            Die();

	}


    void Die()
    {
        opponent.GetComponent<Fight>().FindOpponent("Enemy");
        if (Dead)
        {
            float step = 10 * Time.deltaTime;
            this.transform.position = (Vector3.MoveTowards(this.transform.position, new Vector3(20, this.transform.position.y, 0), step));
            if (Vector3.Distance(this.transform.position, new Vector3(20, this.transform.position.y, 0)) < 1)
            {            
                Debug.Log("Dead");
                Destroy(this.gameObject);
            }
        }
        
    }
}
